package com.kun.index.controller;


import com.kun.common.ResultDto;
import com.kun.index.entity.RateUserBlog;
import com.kun.index.service.impl.RateUserBlogServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户博客评分表 前端控制器
 * </p>
 *
 * @author kunkun
 * @since 2024-03-26
 */
@RestController
@RequestMapping("/index/rate-user-blog")
public class RateUserBlogController {

    @Autowired
    private RateUserBlogServiceImpl rateUserBlogService;


    @PostMapping("/addOrUpdateRate")
    @ApiOperation("新增/更新评分")
    public ResultDto addOrUpdateRate(@RequestBody RateUserBlog rateUserBlog){
        int num=0;
        if (rateUserBlogService.selectByRateUserBlog(rateUserBlog)!=null){     //更新
            num= rateUserBlogService.updateRateUserBlog(rateUserBlog);
        }else{              //新增
            num= rateUserBlogService.addRateUserBlog(rateUserBlog);
        }
        if (num>0){
            return ResultDto.success("新增/更新评分成功");
        }else{
            return ResultDto.fail("新增/更新评分失败");
        }
    }

    @GetMapping("/checkRate")
    @ApiOperation("根据userId和blogId查询评分")
    public ResultDto checkRate(@RequestParam(value = "userId",required = false) Integer userId,
                               @RequestParam(value = "blogId",required = false ) Integer blogId){
        int score=0;
        if (userId==null||blogId==null){
            return ResultDto.success(score);
        }
        RateUserBlog rateUserBlog = new RateUserBlog();
        rateUserBlog.setBlogId(blogId);
        rateUserBlog.setUserId(userId);
        RateUserBlog result = rateUserBlogService.selectByRateUserBlog(rateUserBlog);
        if (result!=null){     //更新
            score= result.getRateScore();
        }
        return ResultDto.success(score);
    }

    @GetMapping("/queryRateByUserId")
    @ApiOperation("根据用户id查询历史浏览记录")
    public ResultDto queryRateByUserId(@RequestParam("pageIndex")Integer pageIndex,
                                          @RequestParam("pageSize")Integer pageSize,
                                          @RequestParam("userId")Integer userId){
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageIndex",pageIndex);
        map.put("pageSize",pageSize);
        map.put("userId",userId);
        map.put("rateScore_desc","desc");

        Map<String, Object> resultMap = rateUserBlogService.queryRateByCondition(map);
        if (resultMap.get("rates")!=null){
            return ResultDto.success(resultMap);
        }else{
            return ResultDto.fail("查询评分失败");
        }
    }


}

